package com.fdzc.controller;

import com.fdzc.model.report;
import com.fdzc.service.reportService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("report")
public class reportController {
    @Autowired
    reportService rService;
    @RequestMapping("back")
    @ResponseBody
    public ModelAndView toReportList(String name,String type){
        ModelAndView mv = new ModelAndView();
        Map<String,String> parm = new HashMap<String,String>();
        //模糊查询关键字
        parm.put("name",name);
        //排序字段设置
        parm.put("type",type);
        //返回的report列表
        List<report> list = rService.getList(parm);
        //设置视图
        mv.setViewName("back");
        mv.addObject("list",list);
        return mv;
    }
    @RequestMapping("save")
    @ResponseBody
    public Map<String,String> addReport(report newReport){
        Map<String,String> result=new HashMap<String, String>();
        rService.save(newReport);
        result.put("code","1");
        result.put("msg","save Success");
        return result;
    }
    @RequestMapping("toLookReport")
    @ResponseBody
    public report toViewReport(Integer id){
        report r=rService.getReportById(id);
        return r;
    }
    @RequestMapping("toDeleteReport")
    @ResponseBody
    public Map<String,String> toDeleteReport(Integer id,String name){
        Map<String,String> result=new HashMap<String, String>();
        rService.delete(id);
        result.put("code","1");
        result.put("name",name);
        return result;
    }
    @RequestMapping("toUpdateReport")
    @ResponseBody
    public Map<String,String> toUpdateReport(report uReport){
        Map<String,String> result=new HashMap<String, String>();
        rService.update(uReport);
        result.put("code","1");
        result.put("msg","save Success");
        return result;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,String> upload(@RequestParam(value = "file", required = false)
                                                 MultipartFile file) throws IOException {
        Map<String,String> map=new HashMap<>();
        // 上传文件路径
        String path = "H:\\学习\\spring\\期末\\upload";
        String filename = file.getOriginalFilename();
        File filepath = new File(path, filename);
        // 判断路径是否存在，如果不存在就创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
        // 将上传文件保存到一个目标文件当中
        file.transferTo(new File(path + File.separator + filename));
        map.put("code","1");
        map.put("filename",filename);
        return map;
    }

    @RequestMapping("/exportInfo")
    public void exportEmpoyeeInfo(HttpServletResponse response ) throws IOException{
        response.setCharacterEncoding("UTF-8");
        List<report> list = rService.queryAllReportInfoToExcel();
        System.out.println(list);
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("学生日报表");

        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("id");
        titleRow.createCell(1).setCellValue("标题");
        titleRow.createCell(2).setCellValue("提交人");
        titleRow.createCell(3).setCellValue("提交时间");
        titleRow.createCell(4).setCellValue("问题");
        titleRow.createCell(5).setCellValue("成就");
        //遍历将数据放到excel列中
        for (report r : list) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(r.getId());
            dataRow.createCell(1).setCellValue(r.getTitle());
            dataRow.createCell(2).setCellValue(r.getUser());
            dataRow.createCell(3).setCellValue(r.getTime());
            dataRow.createCell(4).setCellValue(r.getProblem());
            dataRow.createCell(5).setCellValue(r.getAchievment());
        }
        // 设置下载时客户端Excel的名称
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("学生日报".getBytes(),"iso-8859-1") + ".xls");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
